var b_ser_qtd = true;

$(document).ready(function () {
    
    $('#SER_NOM').focus();
    
    $('#SER_NOM').blur(function(){
        if($(this).val().length == 0) $('#SER_ID,#SER_NOM,#SER_VLR,#SER_QTD,#SER_VLRTOT').val('');
    });
    
    $('#lancar').click(function(){ 
        var html = '';
        
        if($('#SER_ID').val() > 0 && $('#SER_QTD').val() > 0.0 && b_ser_qtd == true){
            
            modal();

            var id = $('#SER_ID').val();
            
            html += '<tr>';
            html += '<td>'+$('#SER_NOM').val()+'</td>';
            html += '<td>'+$('#SER_QTD').val()+' x R$ '+$('#SER_VLR').val()+'</td>';
            html += '<td> R$ '+$('#SER_VLRTOT').val()+'</td>';
            html += '<td><input type="hidden" name="SER_ID[]" value="'+$('#SER_ID').val()+'">';
            html += '<input type="hidden" name="SER_VLR[]" value="'+$('#SER_VLR').val()+'">';
            html += '<input type="hidden" name="SER_VLRTOT[]" value="'+$('#SER_VLRTOT').val()+'" class="valtot">';
            html += '<input type="hidden" name="SER_QTD[]" value="'+$('#SER_QTD').val()+'" class="_'+$('#SER_ID').val()+'">';
            html += '<button type="button" class="btn btn-sm btn-danger remover"><span class="glyphicon glyphicon-floppy-remove" aria-hidden="true"></span> Remover</button></td>';
            html += '</tr>';
            $('#lista').append(html);
            
            var val1 = parseFloat($('#PRS_VLRSUB').val());
            var val2 = parseFloat($('#SER_VLRTOT').val());
            
            if(val1 <= 0.0) val1 = 0;
            if(val2 <= 0.0) val2 = 0;
            
            var TOTAL = val1 + val2;
           
            TOTAL = truncate(TOTAL);           
            $('#PRS_VLR,#PRS_VLRSUB').val(TOTAL); // add total
            
            $('#SER_ID,#SER_NOM,#SER_VLR,#SER_QTD,#SER_VLRTOT').val('');
            remove_modal();
            $('#SER_NOM').focus();
        }
        else if($('#SER_QTD').val() <= 0.0 || b_ser_qtd == false){
            modal_msg('Preencher todas as informações do lançamento!');
            b_ser_qtd = true;
        }
    });
    
    $(document).on("click",".remover", function() {
        modal();
        var valtot = parseFloat($(this).parent().find('.valtot').val());
        var valvnd = parseFloat($('#PRS_VLRSUB').val());
        var TOTAL = valvnd - valtot;
        TOTAL = truncate(TOTAL);
        if(TOTAL <= 0.0) TOTAL = 0;
        $('#PRS_VLR,#PRS_VLRSUB').val(TOTAL);
        $(this).parent().parent().remove(); 
        remove_modal();
    });
    
    $('.find').autocomplete({
        source: URLBASE+'/'+CONTROL+'/buscaservico/',
        minLength: 3,
        select: function( event, ui ) {
            $(".find_id").val(ui.item.id);
            $(".find").val(ui.item.value);
            $(".find_prc").val(ui.item.preco);
            $(".find_qtd").val(1);
            $(".find_qtd").focus();
        }
    });
    
    $('#SER_QTD').blur(function(){
        var val1 = parseFloat($('#SER_VLR').val());
        var val2 = parseFloat($('#SER_QTD').val());
        
        if(val1 <= 0.0) val1 = 0;
        if(val2 <= 0.0) val2 = 0;
        
        var TOTAL = val1 * val2;
        
        TOTAL = truncate(TOTAL);
        $('#SER_VLRTOT').val(TOTAL);
    });
    
    $('#PRS_VLRDSC').blur(function(){
        desconto();
    });
    
    $('.submit').click(function(){
        
        desconto();
        
        if($('#PRS_VLR').val() <= 0.0 || $('#PRS_VLR').val() == ''){
            modal_msg('Total da prestação de serviço deve ser maior que zero!');
            return false;
        }
        else if($('#TMP_ID').val() <= ''){
            modal_msg('Informe o meio de pagamento!');
            return false;
        }
        else {
            document.getElementById("form_edt").submit();
        }
    });
     
});


function truncate(TOTAL){
    //TOTAL = Math.floor(TOTAL * 100) / 100; // trucar numero, erro conforme a quantidade de itens
    //correto
    var res = TOTAL.toString();
    while(res.indexOf('.') != -1) { 
        res = res.split(".",2);
        var TOT = res[0] +'.'+ res[1].substring(0,2); //for (var i in res) alert(res[i]);      
        TOTAL = parseFloat(TOT);
    }
    if(TOTAL <= 0.0 || isNaN(TOTAL)) TOTAL = 0.00;
    return TOTAL;
}

function desconto(){
    var val1 = parseFloat($('#PRS_VLRSUB').val());
    var val2 = parseFloat($('#PRS_VLRDSC').val());

    if(val1 <= 0.0) val1 = 0;
    if(val2 <= 0.0) val2 = 0;

    var TOTAL = val1 - val2;

    TOTAL = truncate(TOTAL);

    if(TOTAL <= 0.0) TOTAL = val1;

    $('#PRS_VLR').val(TOTAL);
}

